package models

import (
	"time"

	"github.com/google/uuid"
)

// TelemetryData represents time-series data from devices
type TelemetryData struct {
	DeviceID  uuid.UUID              `json:"device_id"`
	Timestamp time.Time              `json:"timestamp"`
	Data      map[string]interface{} `json:"data"`
	Location  *Location              `json:"location,omitempty"`
	Quality   DataQuality            `json:"quality"`
}

type DataQuality string

const (
	DataQualityGood    DataQuality = "good"
	DataQualityWarning DataQuality = "warning"
	DataQualityError   DataQuality = "error"
)

// TelemetryMetrics represents aggregated telemetry metrics
type TelemetryMetrics struct {
	DeviceID   uuid.UUID              `json:"device_id"`
	Metric     string                 `json:"metric"`
	TimeWindow string                 `json:"time_window"` // "1m", "5m", "1h", "1d"
	StartTime  time.Time              `json:"start_time"`
	EndTime    time.Time              `json:"end_time"`
	Values     map[string]interface{} `json:"values"` // min, max, avg, sum, count
	Tags       map[string]string      `json:"tags"`
}

// Alert represents a system alert
type Alert struct {
	ID         uuid.UUID     `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	DeviceID   uuid.UUID     `json:"device_id" gorm:"type:uuid"`
	Device     Device        `json:"device" gorm:"foreignKey:DeviceID"`
	RuleID     uuid.UUID     `json:"rule_id" gorm:"type:uuid"`
	Rule       Rule          `json:"rule" gorm:"foreignKey:RuleID"`
	Type       AlertType     `json:"type"`
	Severity   AlertSeverity `json:"severity"`
	Title      string        `json:"title" gorm:"not null"`
	Message    string        `json:"message"`
	Data       AlertData     `json:"data" gorm:"type:jsonb"`
	Status     AlertStatus   `json:"status" gorm:"default:open"`
	AckedBy    *uuid.UUID    `json:"acked_by" gorm:"type:uuid"`
	AckedAt    *time.Time    `json:"acked_at"`
	ResolvedBy *uuid.UUID    `json:"resolved_by" gorm:"type:uuid"`
	ResolvedAt *time.Time    `json:"resolved_at"`
	CreatedAt  time.Time     `json:"created_at"`
	UpdatedAt  time.Time     `json:"updated_at"`
}

type AlertType string

const (
	AlertTypeThreshold    AlertType = "threshold"
	AlertTypeAnomaly      AlertType = "anomaly"
	AlertTypeConnectivity AlertType = "connectivity"
	AlertTypeDeviceHealth AlertType = "device_health"
	AlertTypeSystemHealth AlertType = "system_health"
)

type AlertSeverity string

const (
	AlertSeverityLow      AlertSeverity = "low"
	AlertSeverityMedium   AlertSeverity = "medium"
	AlertSeverityHigh     AlertSeverity = "high"
	AlertSeverityCritical AlertSeverity = "critical"
)

type AlertStatus string

const (
	AlertStatusOpen       AlertStatus = "open"
	AlertStatusAcked      AlertStatus = "acknowledged"
	AlertStatusResolved   AlertStatus = "resolved"
	AlertStatusSuppressed AlertStatus = "suppressed"
)

type AlertData struct {
	TriggerValue   interface{}            `json:"trigger_value"`
	ThresholdValue interface{}            `json:"threshold_value"`
	Context        map[string]interface{} `json:"context"`
	Metadata       map[string]interface{} `json:"metadata"`
}

// EventLog represents system and device events
type EventLog struct {
	ID        uuid.UUID              `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	DeviceID  *uuid.UUID             `json:"device_id" gorm:"type:uuid"`
	Device    *Device                `json:"device" gorm:"foreignKey:DeviceID"`
	UserID    *uuid.UUID             `json:"user_id" gorm:"type:uuid"`
	User      *User                  `json:"user" gorm:"foreignKey:UserID"`
	Type      EventType              `json:"type" gorm:"not null"`
	Action    string                 `json:"action" gorm:"not null"`
	Resource  string                 `json:"resource"`
	Details   map[string]interface{} `json:"details" gorm:"type:jsonb"`
	IPAddress string                 `json:"ip_address"`
	UserAgent string                 `json:"user_agent"`
	Timestamp time.Time              `json:"timestamp"`
}

type EventType string

const (
	EventTypeDevice EventType = "device"
	EventTypeUser   EventType = "user"
	EventTypeSystem EventType = "system"
	EventTypeAPI    EventType = "api"
)

// DataRetentionPolicy represents data retention configuration
type DataRetentionPolicy struct {
	ID            uuid.UUID `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	Name          string    `json:"name" gorm:"not null"`
	DataType      string    `json:"data_type" gorm:"not null"` // "telemetry", "events", "alerts"
	RetentionDays int       `json:"retention_days" gorm:"not null"`
	Enabled       bool      `json:"enabled" gorm:"default:true"`
	CreatedAt     time.Time `json:"created_at"`
	UpdatedAt     time.Time `json:"updated_at"`
}
